package com.hd.crdao.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.hd.crdao.R;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.UIUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/6/19 0019
 */

@SuppressLint("SetJavaScriptEnabled")
public class NewCarDetailsActivity extends Activity {


    public WebView webView;
    public String carinfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardetails);
        initView();
    }

    private void initView() {
        initData();
        webView = (WebView) findViewById(R.id.webview);
        setSettings(webView);
        webView.loadUrl("file:///android_asset/html/car_details_con.html");
        webView.addJavascriptInterface(new ProviderForJavaScript(), "connectBridge");

    }

    private void initData() {
        String carid = getIntent().getStringExtra("carid");
        HashMap<String, String> map = new HashMap<>();
        map.put("appFlag", "true");
        map.put("sign", Constants.SIGN);
        map.put("data", carid);
        OkHttpUtils.post().url(Constants.NEWCARDETAIL_URL).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                carinfo = response;
                ALog.d(carinfo);
            }
        });
    }

    private void setSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAppCacheEnabled(false);
        webView.setWebChromeClient(new WebChromeClient());
    }


    private class ProviderForJavaScript {

        @JavascriptInterface
        public void sendMethod() {
            webView.post(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl("javascript:details(" + carinfo + ")");
                    ALog.d("被JS的安卓方法已经被执行");
                }
            });
        }

        @JavascriptInterface
        public void goBackPressed() {
            finish();
        }

        @JavascriptInterface
        public void buy(String param) {
            Intent intent = new Intent(UIUtils.getContext(), CarBuyDeatailActivity.class);
            intent.putExtra("param", param);
            startActivity(intent);
        }

        @JavascriptInterface
        public void call() {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+getResources().getString(R.string.companyTelephone)));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(webView!=null){
            webView.removeJavascriptInterface("connectBridge");
            webView.destroy();
        }
    }

}

